package com.example.myapp.activities.main_activity.sectionFragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.myapp.R;
import com.example.myapp.activities.show_bills_by_months.ShowBillsByMonth;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * A dummy fragment representing a section of the app, but that simply
 * displays dummy text.
 */
public class DummySectionFragment2 extends Fragment implements AdapterView.OnItemClickListener {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    public static final String ARG_SECTION_NUMBER = "section_number2";

    private View rootView;

    private MonthsAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_main_dummy2,
                container, false);

        initComponent();

        return rootView;
    }

    private void initComponent() {
        ArrayList<String> daty = pobierzDatyNaObecnyRok();

        ListView miesiaceListView = (ListView) rootView.findViewById(R.id.miesiaceListView);
        adapter = new MonthsAdapter(getActivity(), daty);
        miesiaceListView.setAdapter(adapter);
        miesiaceListView.setOnItemClickListener(this);
    }

    private ArrayList<String> pobierzDatyNaObecnyRok() {
        ArrayList<String> daty = new ArrayList<String>();
        Calendar calendar = Calendar.getInstance();
        int rok = calendar.get(Calendar.YEAR);
        for (int i = 1; i <= 12 ; i++) {
            daty.add(i + "." + rok);
        }
        return daty;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.miesiaceListView:
                String date = String.valueOf(adapter.getItem(position));
                Intent intent = new Intent(getActivity(), ShowBillsByMonth.class);
                intent.putExtra("data", date);
                startActivity(intent);
                break;
        }
    }
}

